package es.daw.ejercicio3.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import es.daw.ejercicio3.dao.domain.Contact;

public class ContactDao {


	public ContactDao() {


	}

	public List<Contact> findContacts(String criterio){
		try {
			List<Contact> contactos = new ArrayList<Contact>();
			ResultSet results = null;
			Connection conn = ConnectionFactory.getInstance().getConnection();
			String[] palabras=criterio.split(" ");

			String sql = "SELECT * FROM CONTACT WHERE ";
			for (String palabra : palabras) {
				sql+="nombre LIKE=%"+palabra+"%";
				sql+="OR apellidos LIKE=%"+palabra+"%";
				sql+="OR email LIKE=%"+palabra+"%";
				sql+="OR telefono LIKE=%"+palabra+"% OR ";
			}
			sql.subSequence(0, sql.lastIndexOf("OR"));

			Statement query = conn.createStatement();
			query.execute(sql);

			results =  query.getResultSet();
			while(results.next()){
				Contact c = new Contact();
				c.setNombre(results.getString("nombre"));
				c.setApellidos(results.getString("apellidos"));
				c.setTelefono(results.getString("telefono"));
				c.setEmail(results.getString("email"));

				contactos.add(c);
			}
			results.close();
			conn.close();

			return contactos;
		} catch (SQLException e) {

			e.printStackTrace();
		}

		return null;
	}


	public List<Contact> findContacts(Contact c){
		try {
			List<Contact> contactos = new ArrayList<Contact>();
			ResultSet results = null;
			Connection conn = ConnectionFactory.getInstance().getConnection();

			String sql = "SELECT * FROM CONTACT WHERE ";

			if(c.getNombre()!= null && !c.getNombre().equals(""))
				sql+=" nombre LIKE '%"+ c.getNombre() +"%' AND ";

			if(c.getApellidos()!= null && !c.getApellidos().equals(""))
				sql+=" apellidos LIKE '%"+ c.getApellidos() +"%' AND ";

			if(c.getDireccion()!= null && !c.getDireccion().equals(""))
				sql+=" direccion LIKE '%"+ c.getDireccion() +"%' AND ";

			if(c.getProvincia()!= null && !c.getProvincia().equals(""))
				sql+=" provincia LIKE '%"+ c.getProvincia() +"%' AND ";

			if(c.getCodPostal()!= null && !c.getCodPostal().equals(""))
				sql+=" codpostal LIKE '%"+ c.getCodPostal() +"%' AND ";

			if(c.getTelefono()!= null && !c.getTelefono().equals(""))
				sql+=" telefono LIKE '%"+ c.getTelefono() +"%' AND ";

			sql.subSequence(0, sql.lastIndexOf("AND "));

			Statement query = conn.createStatement();
			query.execute(sql);

			results =  query.getResultSet();
			while(results.next()){
				Contact contact = new Contact();
				contact.setNombre(results.getString("nombre"));
				contact.setApellidos(results.getString("apellidos"));
				contact.setTelefono(results.getString("telefono"));
				contact.setEmail(results.getString("email"));

				contactos.add(contact);
			}
			results.close();
			conn.close();

			return contactos;
		} catch (SQLException e) {

			e.printStackTrace();
		}

		return null;
	}


	public void saveContact(Contact c){
		try {
			Connection conn = ConnectionFactory.getInstance().getConnection();
			String sql=" INSERT INTO CONTACT(nombre,apellidos,telefono, email, codPostal,provincia,direccion) VALUES (";
			sql+=c.getNombre()+",";
			sql+=c.getApellidos()+",";
			sql+=c.getEmail()+",";
			sql+=c.getCodPostal()+",";
			sql+=c.getProvincia()+",";
			sql+=c.getDireccion()+");";

			Statement query = conn.createStatement();
			query.executeUpdate(sql);

		} catch (SQLException e) {

			e.printStackTrace();
		}

	}

}
